<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/19
 * Time: 14:51
 */

namespace App\Http\Controllers\Setting;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\MaterialCategoryModel;
use App\Http\Model\MaterialModel;
use App\Http\Model\MaterialSpecModel;
use App\Http\Model\WarehouseLogInfoModel;
use App\Http\Model\WarehouseModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class MaterialController extends Controller
{
    /**
     * @return \Illuminate\Http\Response
     */
    public function categoryLists(){
        $materialCategoryModel = new MaterialCategoryModel();
        $this->data = $materialCategoryModel->lists();
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function lists(Request $request){
        $rules = [
            'categoryId' => 'nullable|integer',
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:1',
        ];
        $message = [
            'categoryId.integer' => '材料分类参数类型不正确',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only(['categoryId','page','length','search']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialModel = new MaterialModel();
            $materialSpecModel = new MaterialSpecModel();
            $lists = $materialModel->lists($param);
            foreach ($lists as $key => $l){
                $lists[$key]->children = $materialSpecModel->lists(['materialId'=>$l->id]);
            }
            $countLists = $materialModel->countLists($param);
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 271101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addMaterial(Request $request){
        $rules = [
            'name' => 'required',
            'categoryId'=>'required|integer',
            'order' => 'required|integer',
            'code' => 'nullable|string|size:2',
        ];
        $message = [
            'name.required' => '请填写材料名称',
            'categoryId.required' => '获取材料参数类型失败',
            'categoryId.integer' => '材料参数类型错误',
            'order.required' => '请填写材料名称',
            'order.integer' => '请填写材料名称',
            'code.string' => '固定资产材料编码不正确',
            'code.size' => '固定资产材料编码位数不正确',
        ];
        $param = $request->only(['categoryId','name','order','code']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialModel = new MaterialModel();
            if($param['categoryId'] != 4){
                $param['code'] = '00';
            }
            $result = $materialModel->checkRepeat(['name'=>$param['name'],'code'=>$param['code']]);

            if (!empty($result)){
                throw new ApiException('材料名称已经存在，请重新命名', 270401);
            }
            $this->data = $materialModel->addMaterial($param);
        } else {
            throw new ApiException($validator->errors()->first(), 270401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editMaterial(Request $request){
        $rules = [
            'id'=>'required|integer',
            'name' => 'required',
            'order' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取材料参数类型失败',
            'id.integer' => '材料参数类型错误',
            'name.required' => '请填写材料名称',
            'order.required' => '请填写材料名称',
            'order.integer' => '请填写材料名称',
        ];
        $param = $request->only(['id','name','order']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialModel = new MaterialModel();

            $result = $materialModel->checkRepeat(['name'=>$param['name']],$param['id']);
            if (!empty($result)){
                throw new ApiException('材料名称已经存在，请重新命名', 270401);
            }
            $id = $param['id'];
            unset($param['id']);
            $materialModel->update(['id'=>$id],$param);
        } else {
            throw new ApiException($validator->errors()->first(), 270401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function deleteMaterial(Request $request){
        $rules = [
            'id'=>'required|integer',
        ];
        $message = [
            'id.required' => '获取材料参数类型失败',
            'id.integer' => '材料参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialSpecModel = new MaterialSpecModel();
            $specInfo = $materialSpecModel->info(['materialId'=>$param['id']]);
            if (!empty($specInfo)){
                throw new ApiException('材料已经建立规格不能删除', 270401);
            }
            $warehouseModel = new WarehouseModel();
            $warehouse = $warehouseModel->info(['materialId'=>$param['id']]);
            if (!empty($warehouse)){
                throw new ApiException('材料已经被仓库使用，不能删除', 270401);
            }
            $warehouseLogInfoModel = new WarehouseLogInfoModel();
            $warehouseLogInfo = $warehouseLogInfoModel->info(['materialId'=>$param['id']]);
            if (!empty($warehouseLogInfo)){
                throw new ApiException('材料已经产生出入库记录，不能被删除', 270401);
            }
            $materialModel = new MaterialModel();
            $materialModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 270401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editStatus(Request $request){
        $rules = [
            'id'=>'required|integer',
            'status' => 'required|integer|in:0,1',
        ];
        $message = [
            'id.required' => '获取材料参数类型失败',
            'id.integer' => '材料参数类型错误',
            'status.required' => '获取材料状态参数失败',
            'status.integer' => '材料状态参数类型错误',
            'status.in' => '材料状态参数不正确',
        ];
        $param = $request->only(['id','status']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialModel = new MaterialModel();
            $materialModel->editStatus($param['id'],$param['status']);
        } else {
            throw new ApiException($validator->errors()->first(), 270501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function specLists(Request $request){
        $rules = [
            'materialId' => 'required|integer',
        ];
        $message = [
            'materialId.required' => '获取材料参数失败',
            'materialId.integer' => '材料参数类型错误',
        ];
        $param = $request->only(['materialId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialSpecModel = new MaterialSpecModel();
            $lists = $materialSpecModel->lists($param);
            $countLists = $materialSpecModel->countLists($param);
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 270601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addSpec(Request $request){
        $rules = [
            'materialId' => 'required|integer',
            'unitId' => 'required|integer',
            'spec' => 'required',
        ];
        $message = [
            'materialId.required' => '获取材料参数失败',
            'materialId.integer' => '材料参数类型错误',
            'unitId.required' => '获取计量单位参数失败',
            'unitId.integer' => '计量单位参数类型错误',
            'spec.required' => '请填写规格型号',
        ];
        $param = $request->only(['materialId','spec','unitId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialSpecModel = new MaterialSpecModel();
            $result = $materialSpecModel->checkRepeat($param);
            if (!empty($result)){
                throw new ApiException('已经存在相同规格的名称', 270701);
            }
            $materialSpecModel->addSpec($param);
        } else {
            throw new ApiException($validator->errors()->first(), 270701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editSpec(Request $request){
        $rules = [
            'id'=>'required|integer',
            'spec' => 'required',
            'unitId' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取规格参数失败',
            'id.integer' => '规格参数类型错误',
            'spec.required' => '请填写规格',
            'unitId.required' => '获取计量单位参数失败',
            'unitId.integer' => '计量单位参数类型错误',
        ];
        $param = $request->only(['id','spec','unitId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialSpecModel = new MaterialSpecModel();
            $id = $param['id'];
            unset($param['id']);

            $info = $materialSpecModel->info(['id'=>$id]);
            $param['materialId'] = $info['materialId'];
            $result = $materialSpecModel->checkRepeat($param,$id);
            if (!empty($result)){
                throw new ApiException('已经存在相同规格的名称', 270801);
            }
            $materialSpecModel->update(['id'=>$id],$param);
        } else {
            throw new ApiException($validator->errors()->first(), 270801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editSpecStatus(Request $request){
        $rules = [
            'id'=>'required|integer',
            'status' => 'required|integer|in:0,1',
        ];
        $message = [
            'id.required' => '获取规格参数类型失败',
            'id.integer' => '规格参数类型错误',
            'status.required' => '获取规格状态参数失败',
            'status.integer' => '规格状态参数类型错误',
            'status.in' => '规格状态参数不正确',
        ];
        $param = $request->only(['id','status']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialSpecModel = new MaterialSpecModel();
            $materialSpecModel->update(['id'=>$param['id']],['status'=>$param['status']]);
        } else {
            throw new ApiException($validator->errors()->first(), 270801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function deleteSpec(Request $request){
        $rules = [
            'id'=>'required|integer',
        ];
        $message = [
            'id.required' => '获取规格参数失败',
            'id.integer' => '规格参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseModel = new WarehouseModel();
            $warehouse = $warehouseModel->info(['specId'=>$param['id']]);
            if (!empty($warehouse)){
                throw new ApiException('规格已经被仓库使用，不能删除', 270401);
            }
            $warehouseLogInfoModel = new WarehouseLogInfoModel();
            $warehouseLogInfo = $warehouseLogInfoModel->info(['specId'=>$param['id']]);
            if (!empty($warehouseLogInfo)){
                throw new ApiException('规格已经产生出入库记录，不能被删除', 270401);
            }
            $materialSpecModel = new MaterialSpecModel();
            $materialSpecModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 271001);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function selectLists(Request $request){
        $rules = [
            'categoryId' => 'nullable|integer',
        ];
        $message = [
            'categoryId.integer' => '材料分类参数类型不正确',
        ];
        $param = $request->only(['categoryId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialModel = new MaterialModel();
            $lists = $materialModel->selectLists($param);
            $this->data = $lists;
        } else {
            throw new ApiException($validator->errors()->first(), 271101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function specSelectLists(Request $request){
        $rules = [
            'materialId' => 'required|integer',
        ];
        $message = [
            'materialId.required' => '获取材料参数失败',
            'materialId.integer' => '材料参数类型错误',
        ];
        $param = $request->only(['materialId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $materialSpecModel = new MaterialSpecModel();
            $this->data = $materialSpecModel->lists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 270601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}